package com.example.mygis.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.mygis.entity.QyNdxx;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;

@Slf4j
public class TDMapUtil {

    @Value("${gis.tk}")
    private String tk;

    /**
     * 天地图
     * @param item
     */
    private void querylonlat(QyNdxx item) {
        StringBuilder sb = new StringBuilder("http://api.tianditu.gov.cn/search?postStr={\"keyWord\":\"");
        String qymc = item.getAddress();
        if(qymc.contains("号")){
            qymc = qymc.split("号")[0].toString() + '号';
        }
        sb.append(qymc);
        sb.append("\",\"level\":\"11\",\"mapBound\":\"121.5,29.6,122.10,30.0\",\"queryType\":\"1\",\"count\":\"1\",\"start\":\"0\"}&type=query&tk=");
        sb.append(tk);
        HttpResponse response = HttpRequest.get(sb.toString()).execute();
        String body = response.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        JSONArray pois = jsonObject.getJSONArray("pois");
        if(!CollectionUtils.isEmpty(pois)&&pois.get(0) != null){
            JSONObject obj = pois.getJSONObject(0);
            String address = obj.getStr("address");
            String lonlat = obj.getStr("lonlat");
            String[] s = lonlat.split(" ");
            System.out.println(address);
            item.setAddress(address);
            log.info("lon:"+s[0]+"lat:"+s[1]);
            item.setLon(s[0]);
            item.setLat(s[1]);
            item.setSyscFlag("01");
        }else {
            item.setSyscFlag("02");
        }
    }
}
